//
//  TGProgressHUD.m
//
//  Created by Dean on 16/9/9.
//  Copyright © 2016年 Dean. All rights reserved.
//

#import "TGProgressHUD.h"
#import "LEEAlert.h"

#define MBHUD_FONT (17)
@interface LDGifYYImageVIew: YYAnimatedImageView
@end
@implementation LDGifYYImageVIew

- (CGSize)intrinsicContentSize {
    return CGSizeMake(100, 100);
}
@end

@interface RXVideoGifImageVIew: YYAnimatedImageView
@end
@implementation RXVideoGifImageVIew

- (CGSize)intrinsicContentSize {
    return CGSizeMake(50, 50);
}
@end


static TGProgressHUD *_TGHUD = nil;
static YYAnimatedImageView *_GIFimageView = nil;
@implementation TGProgressHUD
+ (void)showMBHUDGifView:(UIView *)view {
    [self showMBHUDGifView:view afterDelay:10];
}
+ (void)showMBHUDGifView:(UIView *)view image:(NSString* ) image  afterDelay:(CGFloat)delay {
    if (_TGHUD) {
        [_TGHUD hideAnimated:YES];
        _TGHUD = nil;
    }
    [self hideGifAnimation];
   if (view==nil) {
      view = [self getFirstView];
   }
//   TGProgressHUD *hud = [TGProgressHUD showHUDAddedTo:view animated:YES];
//   YYImage *yyimage = [YYImage imageNamed:image];
//   LDGifYYImageVIew *cusImageV = [[LDGifYYImageVIew alloc] initWithImage:yyimage];
////    cusImageV.backgroundColor = RGBA(0, 0, 0, 0.6);
//    cusImageV.backgroundColor = UIColorWhite;
//    cusImageV.layer.cornerRadius = 6;
//    cusImageV.contentMode = UIViewContentModeScaleAspectFit;
//    cusImageV.clipsToBounds = YES;
//    cusImageV.image = yyimage;
//     [hud setAnimationType:MBProgressHUDAnimationFade];
//     hud.mode = MBProgressHUDModeCustomView;
//     hud.removeFromSuperViewOnHide = YES;
//     hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
//     hud.bezelView.backgroundColor = [UIColor clearColor];
//     hud.customView = cusImageV;
//     if (delay > 0) {
//         [hud hideAnimated:YES
//                afterDelay:delay];
//     }
//    _TGHUD = hud;
    
    TGProgressHUD *hud = [TGProgressHUD showHUDAddedTo:view animated:YES];
 
      [hud setAnimationType:MBProgressHUDAnimationFade];
      hud.mode = MBProgressHUDModeIndeterminate;
      hud.removeFromSuperViewOnHide = YES;
      hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
      hud.bezelView.backgroundColor = [UIColor clearColor];
//      hud.customView = cusImageV;
      if (delay > 0) {
          [hud hideAnimated:YES
                 afterDelay:delay];
      }
     _TGHUD = hud;
}

+ (void)showMBHUDVideoGifView:(UIView *)view image:(NSString* ) image  afterDelay:(CGFloat)delay {
    if (_TGHUD) {
        [_TGHUD hideAnimated:YES];
        _TGHUD = nil;
    }
    [self hideGifAnimation];
   if (view==nil) {
      view = [self getFirstView];
   }
   TGProgressHUD *hud = [TGProgressHUD showHUDAddedTo:view animated:YES];
   YYImage *yyimage = [YYImage imageNamed:image];
    RXVideoGifImageVIew *cusImageV = [[RXVideoGifImageVIew alloc] initWithImage:yyimage];
    cusImageV.backgroundColor = RGBA(0, 0, 0, 0.6);
    cusImageV.layer.cornerRadius = 6;
    cusImageV.contentMode = UIViewContentModeScaleAspectFit;
    cusImageV.clipsToBounds = YES;
    cusImageV.image = yyimage;
    hud.userInteractionEnabled = NO;
     [hud setAnimationType:MBProgressHUDAnimationFade];
     hud.mode = MBProgressHUDModeCustomView;
     hud.removeFromSuperViewOnHide = YES;
     hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
     hud.bezelView.backgroundColor = [UIColor clearColor];
     hud.customView = cusImageV;
     if (delay > 0) {
         [hud hideAnimated:YES
                afterDelay:delay];
     }
    _TGHUD = hud;
}
+ (void)showMBHUDGifView:(UIView *)view afterDelay:(CGFloat)delay {
    [self showMBHUDGifView:view image:@"loading@2x.gif" afterDelay:delay];
}

+ (void)setEnableTouch {
    [_TGHUD setUserInteractionEnabled:NO];
}
+ (void)showGifAnimationDelay:(NSInteger)delay {
    
    [self showGifAnimation];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hideGifAnimation];
    });
}
+ (void)showGifAnimation {
    [self hideGifAnimation];
    YYImage *image = [YYImage imageNamed:@"loading@2x.gif"];
    _GIFimageView = [[YYAnimatedImageView alloc] init];
    _GIFimageView.backgroundColor = RGBA(0, 0, 0, 0.6);
    _GIFimageView.layer.cornerRadius = 6;
    _GIFimageView.clipsToBounds = YES;
    _GIFimageView.image = image;
    _GIFimageView.frame = CGRectMake(0, 0, 100, 100);
    [_GIFimageView showInWindow];
}
+ (void)hideGifAnimation {
    if (_GIFimageView) {
        [_GIFimageView hideInWindow];
        _GIFimageView = nil;
    }
}

+ (UIWindow *)window
{
    return APPDelegate.window;
}

+ (NSArray*)windows{
    return [UIApplication sharedApplication].windows;
}



+ (void)showMBHUDErrorTip:(NSString *)tip {
    [self hideGifAnimation];
    [self showCustomIcon:@"MBHUD_Error" Title:tip ToView:nil];
}
+ (void)showMBHUDInfoTip:(NSString *)tip {
    [self showCustomIcon:@"MBHUD_Warn" Title:tip ToView:nil];
    
}
+ (void)showMBHUDSuccessTip:(NSString *)tip {
    [self showCustomIcon:@"MBHUD_Success" Title:tip ToView:nil];
    
}
+ (void)showMBHUDSmileTip:(NSString *)tip {
    [self showCustomIcon:@"MBHUD_InfoSmile" Title:tip ToView:nil];
    
}

+ (void)showHUDSucessTitle:(NSString *)tip  {
    [self hideGifAnimation];
    [self showCustomIcon:@"MBSuccess_blue" Title:tip detailTitle:nil ToView:nil afterDelay:2.0];
}
+ (void)showHUDSucessTip:(NSString *)tip {
    [self showHUDSucessTip:tip  view:nil];
}
+ (void)showHUDSucessTip:(NSString *)tip view:(UIView*)view {
    [self showCustomIcon:@"MBSuccess_blue" Title:tip ToView:view];
}
+ (void)showCustomIcon:(NSString *)iconName Title:(NSString *)title detailTitle:(NSString *)dtitle ToView:(UIView *)view afterDelay:(CGFloat)delay {
    if (_TGHUD) {
        [_TGHUD hideAnimated:YES];
        _TGHUD = nil;
    }
    [self hideGifAnimation];
     if (view==nil) {
         view = [self getFirstView];
     }
    _TGHUD = [self showHUDAddedTo:view animated:YES];
    _TGHUD.mode = MBProgressHUDModeCustomView;
    UIImage *image = [UIImage imageNamed:iconName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.image = image;
    _TGHUD.customView = imageView;
    [_TGHUD setAnimationType:MBProgressHUDAnimationZoom];
    _TGHUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    _TGHUD.bezelView.color = RGBCOLOR(0, 0, 0, 0.8);
    _TGHUD.contentColor = [UIColor whiteColor];
    if (title) {
        _TGHUD.label.text = title;
    }
    if (dtitle) {
        _TGHUD.detailsLabel.text = dtitle;
    }
    [_TGHUD hideAnimated:YES afterDelay:delay];
}
+ (void)showCustomIcon:(NSString *)iconName Title:(NSString *)title ToView:(UIView *)view {
  
    [self showCustomIcon:iconName Title:nil detailTitle:title ToView:view afterDelay:HUD_DEFAULT_TIME];
    
}
+(void)setProgessText:(NSString *)text {
    if (_TGHUD) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            _TGHUD.detailsLabel.text = text;
        }];
    }
}
+ (void)showHUDTipNetError{
    [self hideGifAnimation];
    [TGGCD executeMainAsyncTask:^{
        [self showHUDTip:NetworkError];
    }];
}
+ (void)showWhiteHUDTip:(NSString *)tip {
   

    UIView *view = [self getFirstView];
    _TGHUD = (TGProgressHUD *)[self newHUD:tip view:view delegate:nil];
    [_TGHUD setMode:MBProgressHUDModeText];
    [_TGHUD showAnimated:YES];
    _TGHUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    _TGHUD.bezelView.color = [UIColor whiteColor];
    _TGHUD.contentColor = [UIColor blackColor];
    _TGHUD.backgroundView.backgroundColor = RGBCOLOR(0, 0, 0, 0.6);
    [_TGHUD hideAnimated:YES afterDelay:[self displayDurationForString:tip]];
}
+ (void)showHUDTip:(NSString *)text
{
    [self hideGifAnimation];
    [self showHUDTip:text view:nil];
}

+ (NSTimeInterval)displayDurationForString:(NSString*)string {
    
    NSInteger max  = MAX((float)string.length * 0.075 + 0.6, 1.4);
    if (max > 5) {
        return 5;
    }
    return max;
}
+ (void)showHUDTip:(NSString *)txt view:(UIView *)view
{
    [self hideGifAnimation];
    
    [self showHUDTip:txt view:view afterDelay:[self displayDurationForString:txt]];
}
+ (void)showHUDTip:(NSString *)txt view:(UIView *)view afterDelay:(CGFloat)delay
{
     if (view==nil) {
         view = [self getFirstView];
      }
    MBProgressHUD *HUD = [self newHUD:txt view:view delegate:nil];
    [HUD setMode:MBProgressHUDModeText];
    [HUD showAnimated:YES];
    [HUD hideAnimated:YES afterDelay:delay];
}
+ (void)showHUD:(NSString *)title view:(UIView *)view {
    [self showMBHUDGifView:view];
}
+ (void)showHUD:(NSString *)title view:(UIView *)view afterDelay:(CGFloat)delay {
    MBProgressHUD *HUD = [self newHUD:title  view:view delegate:nil];
    [HUD showAnimated:YES];
    [HUD hideAnimated:YES afterDelay:delay];
}
+ (void)showHUD:(NSString *)title {
    [self showHUD:title view:nil];
}
+ (TGProgressHUD *)newHUD:(NSString *)text view:(UIView *)view delegate:(id<MBProgressHUDDelegate>)delegate
{
    if (view==nil) {
        view = [self getFirstView];
    }
    if (_TGHUD) {
        [_TGHUD hideAnimated:NO];
        _TGHUD = nil;
    }
    _TGHUD = [[TGProgressHUD alloc] initWithView:view];
    [_TGHUD setRemoveFromSuperViewOnHide:YES];
    [_TGHUD setDelegate:delegate];
    [_TGHUD setAnimationType:MBProgressHUDAnimationZoom];
    [view addSubview:_TGHUD];
    [view bringSubviewToFront:_TGHUD];
    _TGHUD.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    _TGHUD.bezelView.color = RGBCOLOR(0, 0, 0, 0.8);
    _TGHUD.contentColor = [UIColor whiteColor];
    [_TGHUD setMode:MBProgressHUDModeIndeterminate];
    if([text length]>0){
        _TGHUD.detailsLabel.text = text;
    }
    return _TGHUD;
}
+ (void)dismissMPHUD {
    [self hideGifAnimation];
    if (_TGHUD) {
        [_TGHUD hideAnimated:YES];
        _TGHUD = nil;
    }
}
+ (void)hideMBHUD:(BOOL)animation {
    if (_TGHUD) {
        [_TGHUD hideAnimated:animation];
        _TGHUD = nil;
    }else{
        [self dismissMBHUD:nil];
    }
}
+ (void)dismissMBHUD:(UIView *)view {
    [self hideGifAnimation];
    if (view==nil) {
        view = [self window];
    }
    [MBProgressHUD hideHUDForView:view animated:YES];
}

+ (UIView *)getFirstView {
        UIView *view = nil;
        if (IQKeyboardManager.sharedManager.keyboardShowing) {
              view = [[self windows] lastObject];
          }else {
              if ([LEEAlert getAlertWindow].hidden==NO) {
                      view = [LEEAlert getAlertWindow];
                  } else {
                    view = [self frontWindow];
               }
          }
    return  view;
}
+ (UIWindow *)frontWindow {

    NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator];
    for (UIWindow *window in frontToBackWindows) {
        BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen;
        BOOL windowIsVisible = !window.hidden && window.alpha > 0;
        BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal;
        
        if(windowOnMainScreen && windowIsVisible && windowLevelNormal) {
            if ([window isKindOfClass:[LEEAlertWindow class]]) {
                return [self window];
            }
            return window ;
            break;
        }
    }
    return [self window];
}


@end
